package cn.hncj.lyc.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import cn.hncj.lyc.bean.UserInfo;
import cn.hncj.lyc.dao.UserDao;
import cn.hncj.lyc.dao.UserInfoMapper1;
import cn.hncj.lyc.service.UserService;
import cn.hncj.lyc.utils.IdWorker;
@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDao userDao;
	@Autowired
	BCryptPasswordEncoder encoder;
	@Autowired
	private IdWorker idWorker;
	@Autowired
	private UserInfoMapper1 userInfoMapper1;

	/**
	 * 根据手机号和密码查询用户
	 * @param mobile
	 * @param password
	 * @return
	 */
	public UserInfo findByMobileAndPassword(String mobile,String password){
		UserInfo user = userDao.findByMobile(mobile);
		if(user!=null && encoder.matches(password,user.getUserPw())){
			return user;
		}else{
			return null;
		}
	}

	/**
	 * 根据手机号查询
	 * @param mobile
	 * @return
	 */
	public UserInfo findByMobile(String mobile){
		return userDao.findByMobile(mobile);
	}


	/**
	 * 通过姓名和密码查人
	 * @param name
	 * @param password
	 * @return
	 */
	public UserInfo findByNameAndPassword(String name,String password){
		UserInfo user = userDao.findByName(name);
		if (user!=null && encoder.matches(password,user.getUserPw())){
			return user;
		}else {
			return null;
		}

	}
	/**
	 * 注册添加用户
	 * @param user
	 */
	public void addUser(UserInfo user) {
		user.setUserId(idWorker.nextId()+"");
		String newpassword = encoder.encode(user.getUserPw());//加密后的密码
		user.setUserPw(newpassword);
		userInfoMapper1.addUserInfo(user);
	}
	
	//获取登录信息
	public UserInfo doLogin(UserInfo userInfo) {
		return userDao.getUserInfo(userInfo);
	}

}
